//绘制文本背景图标
const roundRectBackground = function (str, padding, fontSize, fontColor, backgroundColor, borderRadius) {
    let [width, height] = getTextSize(str);
    padding = padding || [4, 0, 4, 0];
    width = width + padding[1] + padding[3];
    height = height + padding[0] + padding[2];
    let canvas = document.createElement("canvas");
    canvas.width = width;
    canvas.height = height;
    let context = canvas.getContext("2d");
    fontSize = fontSize || 14;
    // fontColor = fontColor || '#000';
    fontColor = fontColor || '#3285FF';
    backgroundColor = backgroundColor || '#FFF';
    borderRadius = borderRadius || (height / 2);
    context.beginPath();
    context.moveTo(0 + borderRadius, 0);
    context.arcTo(0 + width, 0, 0 + width, 0 + height, borderRadius);
    context.arcTo(0 + width, 0 + height, 0, 0 + height, borderRadius);
    context.arcTo(0, 0 + height, 0, 0, borderRadius);
    context.arcTo(0, 0, 0 + width, 0, borderRadius);
    context.closePath();
    context.fillStyle = backgroundColor;
    context.fill();
    // context.fontSize = fontSize + 'px';
    context.font = fontSize + 'px 微软雅黑';
    context.fillStyle = fontColor;
    context.textAlign = "center";
    context.textBaseline = "middle";
    context.fillText(str, width / 2, height / 2);
    let dataURL = canvas.toDataURL("image/png");
    return dataURL;
}

//计算字符串像素长度
function getTextSize(str, fontSize) {
    let width = 0,
        height = 0;
    let ele = document.createElement("div");
    ele.style.position = "absolute";
    ele.style.whiteSpace = "nowrap";
    ele.style.fontSize = fontSize || 12 + 'px';
    ele.style.opacity = 0;
    ele.innerText = str;
    document.body.append(ele);
    width = ele.getBoundingClientRect().width;
    height = ele.getBoundingClientRect().height;
    document.body.removeChild(ele);
    return [width, height];
}


export {
    roundRectBackground
}
